package com.gduf.lsm.controller;

import com.gduf.lsm.common.Result;
import com.gduf.lsm.entity.News;
import com.gduf.lsm.service.NewsService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @Author lsm
 * @Data
 */

/**
 * 咨询信息前端操作接口
 **/
@RestController
@RequestMapping("/news")

public class NewsCotroller {
    @Autowired
    private NewsService newsService;

    /**
     * 新增
     * @param news
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody News news) {
        newsService.add(news);
        return Result.success();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id) {
        newsService.deleteById(id);
        return Result.success();
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        newsService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 修改
     * @param news
     * @return
     */
    @PutMapping("/update")
    public Result updateById(@RequestBody News news) {
        newsService.updateById(news);
        return Result.success();
    }

    /**
     * 修浏览量
     * @param id
     * @return
     */
    @PutMapping("/updateCount/{id}")
    public Result updateCount(@PathVariable Integer id) {
        newsService.updateCount(id);
        return Result.success();
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        News news = newsService.selectById(id);
        return Result.success(news);
    }

    /**
     * 查询所有
     * @param news
     * @return
     */
    @GetMapping("/selectAll")
    public Result selectAll(News news ) {
        List<News> list = newsService.selectAll(news);
        return Result.success(list);
    }

    /**
     * 分页查询
     * @param news
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/selectPage")
    public Result selectPage(News news,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<News> page = newsService.selectPage(news, pageNum, pageSize);
        return Result.success(page);
    }

    /**
     * 查询最新最热数据
     * @param sort
     * @return
     */
    @GetMapping("/selectTopNews")
    public Result selectTopNews(@RequestParam String sort){
        List<News> list=newsService.selectTopNews(sort);
        return Result.success(list);
    }
}
